import React from 'react';
import { Route, Redirect } from 'react-router-dom';

var formstate = {}, token = undefined;

const AuthRoute = ({ component: Component, ...rest }) => {
    if (localStorage.getItem("formstate")) {
        formstate = JSON.parse(localStorage.getItem("formstate"));
        token = formstate.username + formstate.password;
    }
    return(
        <Route
            {...rest}
            render={props => {
                return token ? (
                    <Component {...props}/>
                ) : (
                    <Redirect to="/login" />
                )
            }}
        />
    )
}

export default AuthRoute;